/*
 * Filename:    ProdeuctClearJobServiceImpl.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-4-1
 */
package com.paic.mhis.hcpms.productClear.biz.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.hcpms.productClear.biz.service.ProductClearJobService;
import com.paic.mhis.hcpms.productClear.constants.ProductClearConstants;
import com.paic.mhis.hcpms.productClear.constants.ProductConstants;
import com.paic.mhis.hcpms.productClear.dao.ProductClearDAO;
import com.paic.mhis.hcpms.productClear.dao.ProductClearJobDao;
import com.paic.mhis.hcpms.productClear.dto.ProductClearDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.KpiJobCtrlService;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.constants.CalculationJobConstants;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.HcpmsKpiJobCtrlDTO;
import com.pingan.um.client.util.UUID;

@Component("productClearJobService")
public class ProductClearJobServiceImpl implements ProductClearJobService {
	public static Log log = LogFactory.getLog(ProductClearJobServiceImpl.class);
	@Autowired
	private ProductClearJobDao productClearJobDao;

	@Autowired
	private KpiJobCtrlService kpiJobCtrlService;
	@Autowired
	private ProductClearDAO productClearDAO;

	@Override
	@Transactional
	public void prodeuctSumData() {
		log.info("------------剔除费用数据汇总JOB进入-----------");
		HcpmsKpiJobCtrlDTO ctrlDto = new HcpmsKpiJobCtrlDTO();
		ctrlDto.setJobCode("J20150401007");
		ctrlDto.setJobName("剔除费用数据汇总JOB");
		ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_USE);
		ctrlDto.setTmpId(UUID.getID());
		ctrlDto = kpiJobCtrlService.getHcpmsJobCtrl(ctrlDto);
		if (ctrlDto == null) {
			log.info("-----剔除费用数据汇总JOB被占用-----");
			return;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			String times = null;
			times = productClearJobDao.queryTimeByType(ProductConstants.TIME_TYPE);
			if (StringUtils.isEmpty(times)) {
				times = sdf.format(new Date());
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(times));
			cal.add(Calendar.MONTH, -1);
			map.put("startDate", cal.getTime());
			cal.add(Calendar.MONTH, 1);
			map.put("endDate", cal.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<ProductClearDTO> auditData = productClearJobDao.queryAuditCaseData(map);
		if (null != auditData && auditData.size() > 0) {
			for (ProductClearDTO dto : auditData) {
				dto.setProductType(ProductConstants.PRODUCT_TYPE_JH);
				productClearJobDao.updateProductClear(dto);
				productClearJobDao.insertProductClear(dto);
				productClearDAO.updateAuditCaseInfo(ProductClearConstants.status_11, dto.getCode());
			}
		} else {
			log.info("--------------稽核案件无剔除费用汇总数据------------");
		}
		List<ProductClearDTO> caseData = productClearJobDao.queryWarnInfoData(map);

		if (null != caseData && caseData.size() > 0) {
			for (ProductClearDTO dto : caseData) {
				dto.setProductType(ProductConstants.PRODUCT_TYPE_YJ);
				productClearJobDao.updateProductClear(dto);
				productClearJobDao.insertProductClear(dto);
				productClearDAO.updateWaringInfo(ProductClearConstants.status_11, dto.getCode());
			}
		} else {
			log.info("--------------预警案件无剔除费用汇总数据------------");
		}

		log.info("--------------剔除费用数据汇总JOB结束------------");
		ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_IDLE);
		ctrlDto.setTmpId(null);
		kpiJobCtrlService.updateHcpmsJobCtrl(ctrlDto);

	}
}
